/**
 * Created by Administrator on 2017/1/11.
 */
$(function () {
    getUrl("myAD/applicationAD/search");
    $("#agree").click(function () {
        var flag = $("#agree_img").attr("flag");
        if (flag == 1) {
            $("#agree_img").attr("src", "/Lvscore_User_M/userWeb/static/images/contright_icon3.png");
            $("#agree_img").attr("flag", 0);
            $("#bindOk").attr("style", "opacity: 0.5;pointer-events: none;");

        } else {
            $("#agree_img").attr("src", "/Lvscore_User_M/userWeb/static/images/reminder_icon.png");
            $("#agree_img").attr("flag", 1);
            $("#bindOk").attr("style", "");
        }
    });
    $("#account").blur(function () {
        var account = $("#account").val().replace(/\s+/g, "");
        $("#account").val(account);
        isAccount(account, null);
    });
    $("#account2").blur(function () {
        var account = $("#account").val().replace(/\s+/g, "");
        $("#account").val(account);
        var account2 = $("#account2").val().replace(/\s+/g, "");
        $("#account2").val(account2);
        isAccount(account, account2);
    });
    var cookie = new sweetCookie();
    var appInfo = decodeURIComponent(cookie.getCookie("AppInfo")).split(":");
    if (appInfo.length == 2) {
        $("#appName").text(appInfo[1]);
        $("#bindOk").click(function () {
            var account = $("#account").val().replace(/\s+/g, "");
            $("#account").val(account);
            var account2 = $("#account2").val().replace(/\s+/g, "");
            $("#account2").val(account2);
            if (isAccount(account, account2)) {
                bindApp(appInfo[0], account);
            }
        });
    }
});
function isAccount(account, account2) {
    if (account.length <= 0) {
        $("#accountError").html('<img style="margin-right: 3px;margin-bottom: 2px" width="16px" height="16px" src="/Lvscore_User_M/userWeb/static/images/turnBankcard_icon.png" alt="">注册账号不能为空');
    } else if (getLength(account) > 50) {
        $("#accountError").html('<img style="margin-right: 3px;margin-bottom: 2px" width="16px" height="16px" src="/Lvscore_User_M/userWeb/static/images/turnBankcard_icon.png" alt="">注册账号过长');
    } else {
        $("#accountError").html("");
        if (account2 != null && account2 != account) {
            $("#accountError2").html('<img style="margin-right: 3px;margin-bottom: 2px" width="16px" height="16px" src="/Lvscore_User_M/userWeb/static/images/turnBankcard_icon.png" alt="">两次输入账号不一致');
        } else {
            $("#accountError2").html("");
            return true;
        }
    }
    return false;
}
function bindApp(appId, account) {
    /**
     * 绑定应用
     * @param appId 应用ID
     * @param account 账户名称
     */
    var url = "/Lvscore_User_M/myAds/ajaxBindAppAccount";
    $.post(url, {
        app_id: appId,
        account: account
    }, function (data) {
        if (data == 1) {
            alertBox("绑定成功");
            setTimeout("window.location.href = '/Lvscore_User_M/userWeb/views/myAD/applicationAD/index.html'", 2000);
        } else if (data == -1) {
            alertBox("绑定失败，请稍后重试！");
        } else {
            alertBox("服务器异常，请稍后重试！");
        }
    });
}